#!/usr/bin/python
# dummy script for checking for missing btsnoop files
import os
import re
import argparse
import collections

arg_parser = argparse.ArgumentParser(description=
	"Check for missing btsnoop files against local bluez.git")
arg_parser.add_argument("-b", "--bluez", nargs='+', required=True,
	type=argparse.FileType('r'), help="Path to local pts-xxx.txt files")

try:
        args = arg_parser.parse_args()
except IOError, msg:
        arg_parser.error(str(msg))

list_btsnoop_files = list()
list_test_cases = list()
btsnoop_files = [f for f in os.listdir('.') if os.path.isfile(f)]

missing = list()

## prepare proper variable(s) for iterating over btsnoop files
## returns list() of btsnoop files in github
for (i, item) in enumerate(args.bluez):
	for j in args.bluez[i].name.split('/'):
		if "pts" in j:
			profile_arg = j.translate(None, '-.')
			profile_arg = re.sub('pts', '', profile_arg).replace('txt', '').upper()
			print profile_arg
			for f in btsnoop_files:
				if profile_arg in f:
					## dirty way of removing XXX__
					f_1 = f.replace(profile_arg + '__', '')
					## dirty way of removing .btsnoop
					f_2 = f_1.replace('.btsnoop', '')
					list_btsnoop_files.append(f_2);
	args.bluez[i].name.strip()

## next step is to get a list() of respective tests from local git
## pts-xxx.txt files
for (i, item) in enumerate(args.bluez):
        line = args.bluez[i].readline()
	while line != "":
		if ("TC_") in line:
			if ("PASS") in line:
				test_signature = line.split('\t')
				## we should not add a whole line; just the test name
				list_test_cases.append(test_signature[0])
		line = args.bluez[i].readline()

## now just compare both lists
## and print the diff
#for test_case in list_test_cases:
#	for btsnoop in list_btsnoop_files:
#		if (test_case in btsnoop):
#			print "not missing " + test_case
missing = (collections.Counter(list_test_cases) == collections.Counter(list_btsnoop_files))
if collections.Counter(list_test_cases) == collections.Counter(list_btsnoop_files):
	print "OK. No mismatch between btsnoops and given pts results"
else:
	print "There is a mismatch between btsnoops and pts results"

for item in missing:
	print item

#for item in list_btsnoop_files:
#	print item

#for item in list_test_cases:
#        print item
